// pages/settlement/settlement.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 判断是否有地址
    isShow: true,
    addressList: [],
    settlementList: [],
    // 总的数量和总价
    totalNum: 1,
    totalPrice: 0,
    // 地址的下标
    index: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    if (options.id != undefined) {
      this.setData({
        index: options.id
      })
    }
    // console.log(this.data.index)
    // 给每个购买的商品赋值序号
    var number = wx.getStorageSync('number')
    if (number.length == 0) {
      wx.setStorageSync('number', [0])

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var addressList = wx.getStorageSync('addressList')
    var isShow = true
    var addressList1 = []
    // 判断是否有地址
    if (addressList.length != 0) {
      isShow = false,
        addressList1 = [addressList[this.data.index]]
    }
    var settlementList = wx.getStorageSync('settlementList')
    var totalNum = 0
    var totalPrice = 0
    // 计算商品总数和总价
    for (var i = 0; i < settlementList.length; i++) {
      totalNum += settlementList[i].num
      totalPrice += settlementList[i].num *
        settlementList[i].price
    }

    this.setData({
      isShow,
      addressList: addressList1,
      settlementList,
      totalNum,
      totalPrice
    })
  },


  // 地址为空的点击事件
  toAddSite: function () {
    wx.navigateTo({
      url: '/pages/addSite/addSite?index=-1',
    })
  },


  // 地址不为空的点击事件
  toAddress: function () {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },


  toPay: function () {
    var _this = this
    var settlementList = wx.getStorageSync('settlementList')
    var carList = wx.getStorageSync('carList')
    var addressList = wx.getStorageSync('addressList')
    // 删除购物车中当前的商品
    for (var i = settlementList.length - 1; i >= 0; i--) {
      // console.log(settlementList[i].checked)
      // 如果是从购物车中结算的商品，则执行里面的内容
      if (settlementList[i].checked != undefined) {
        // console.log(settlementList[i])
        for (var j = carList.length - 1; j >= 0; j--) {
          // 判断当前商品和购物车中哪些商品的id相同，相同则删除购物车中的数据
          if (settlementList[i].id == carList[j].id) {
            carList.splice(i, 1)
          }
        }
      }
    }
    // console.log(carList)
    wx.setStorageSync('carList', carList)

    // 判断地址是否为空，空的时候显示提示信息添加地址
    if (addressList.length == 0) {
      wx.showToast({
        title: '请添加地址',
        icon: "none",
        duration: 2000
      })
    } else {
      // 有地址的时候，显示是否确定付款的Model框
      wx.showModal({
        title: '总价：' + this.data.totalPrice,
        content: '是否确认付款',
        success(res) {
          if (res.confirm) {

            console.log('用户点击确定')
            // 获取序号
            var number = wx.getStorageSync('number')

            // 将当前商品放入全部订单的缓存数据里
            var allorderList = wx.getStorageSync('allorderList')
            _this.fun(number, '确认收货', 'allorderList', allorderList)

            // 将当前商品放入待收货的缓存数据里
            var receviedList = wx.getStorageSync('receviedList')
            _this.fun(number, '确认收货', 'receviedList', receviedList)

            // 页面跳转
            wx.navigateTo({
              url: '/pages/order/order?index=2',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
            // 获取序号
            var number = wx.getStorageSync('number')

            // 将当前商品放入全部订单的缓存数据里
            var allorderList = wx.getStorageSync('allorderList')
            _this.fun(number, '去付款', 'allorderList', allorderList)

            // 将当前商品放入待付款的缓存数据里
            var paymentList = wx.getStorageSync('paymentList')
            _this.fun(number, '去付款', 'paymentList', paymentList)

            wx.navigateTo({
              url: '/pages/order/order?index=1',
            })
          }
        }
      })

    }
  },

  // 封装的函数
  fun: function (number, btn, str, good) {
    var settlementList = wx.getStorageSync('settlementList')
    var settlementList1 = []
    for (var i = 0; i < settlementList.length; i++) {
      number++
      var item = {
        number: number,
        id: settlementList[i].id,
        img: settlementList[i].img,
        title: settlementList[i].title,
        desc: settlementList[i].desc,
        price: settlementList[i].price,
        num: settlementList[i].num,
        btn: btn
      }
      if (good.length == 0) {
        if (settlementList.length == 1) {
          wx.setStorageSync(str, [item])
        } else {
          settlementList1.unshift(item)
          wx.setStorageSync(str, settlementList1)
        }
      } else {
        good.unshift(item)
        wx.setStorageSync(str, good)
      }
    }
    wx.setStorageSync('number', [number])
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})